<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.8 Reflection</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:51:13 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>What's New in Java 1.1</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_09.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-4-SECT-8">4.8 Reflection</A></h2>

<P CLASS=para>
Reflection in Java 1.1 refers to the ability of Java classes
to reflect upon themselves, or to "look inside themselves."
The <tt CLASS=literal>java.lang.Class</tt> class has been greatly enhanced
in Java 1.1.  It now includes methods that return
the fields, methods, and constructors defined by a class.
These items are returned as objects of type <tt CLASS=literal>Field</tt>,
<tt CLASS=literal>Method</tt>, and <tt CLASS=literal>Constructor</tt>, respectively.
These new classes are part of the new
<tt CLASS=literal>java.lang.reflect</tt> package, and they each provide
methods to obtain complete information about the field,
method, or constructor they represent.  For example, the
<tt CLASS=literal>Method</tt> object has methods to query the name,
the parameter types, and the return type
of the method it represents. <A HREF="ch12_01.htm">Chapter 12, <i>Reflection</i></A>
provides some examples of using the Reflection API.

<P CLASS=para>
Besides allowing a program to inspect the members
of a class, the <tt CLASS=literal>java.lang.reflect</tt> package
also allows a program to manipulate these fields and
methods.  The <tt CLASS=literal>Field</tt> class defines methods that get
and set the value of the represented field for any given
object of the appropriate
type.  Similarly, the <tt CLASS=literal>Method</tt> object defines an
<tt CLASS=literal>invoke()</tt> method
that allows the represented method to be invoked, and the
<tt CLASS=literal>Constructor</tt> class defines a <tt CLASS=literal>newInstance()</tt>
method that creates a
new object and invokes the represented constructor on it.
<tt CLASS=literal>java.lang.reflect</tt> also defines an <tt CLASS=literal>Array</tt>
class.  It does not represent a specific
array, but defines static methods that read and write array
elements and dynamically create new arrays.

<P CLASS=para>
With the addition of reflection, the <tt CLASS=literal>Class</tt> class has
been
expanded to represent not just Java classes, but any Java
type, including primitive types and array types.  There is a
special <tt CLASS=literal>Class</tt> object that represents each of the eight
Java primitive types, and another special <tt CLASS=literal>Class</tt> object that
represents the <tt CLASS=literal>void</tt> type.  These special
<tt CLASS=literal>Class</tt> objects are available as constants in the
wrapper objects for the primitive types.
<tt CLASS=literal>Integer.TYPE</tt> is a <tt CLASS=literal>Class</tt> object that
represents the <tt CLASS=literal>int</tt> type, for example, and
<tt CLASS=literal>Void.TYPE</tt> is a <tt CLASS=literal>Class</tt> object that represents
the <tt CLASS=literal>void</tt> type.

<P CLASS=para>
Finally, new Java language syntax makes it easier to
obtain a <tt CLASS=literal>Class</tt> object that represents a Java class.
If you follow the name of a class, interface, or other type
with <tt CLASS=literal>.class</tt>, Java evaluates that expression and
returns the corresponding <tt CLASS=literal>Class</tt> object.  So, for
example, the following two expressions are equivalent:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
String.class
Class.forName("java.lang.String")
</PRE>
</DIV>

<P CLASS=para>
Note that this syntax also works with primitive type names:
you can write <tt CLASS=literal>short.class</tt>, for example, which
returns the same value as <tt CLASS=literal>Short.TYPE</tt>.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_09.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Object Serialization</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Java Beans</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
